﻿using FPSO.Models.Constant;
using FPSO.Models.TagMapping;
using Microsoft.Practices.Prism.ViewModel;
using System;

namespace FPSO.Models.ViewModel.Controls
{

    public class TankOilViewModel : NotificationObject
    {
        private double tempsensor_bottom_z = 1.5;
        private double tempsensor_mid_z = 10.0;
        private double tempsensor_top_z = 18.0;
        public TankOilViewModel(string tankId, double totalVol)
        {
            TankId = tankId;
            totalVolume = totalVol;
            volume = 0d;
            level = 0d;
            level1 = 0d;
            rate = 0d;
            density = 0d;
            temperature = 0d;
			press = 0d;
			bottomTemperature = 0d;
			midTemperature = 0d;
			topTemperature = 0d;
		}

		private double volume;
        private double level;
        private double level1;
        private double rate;
        private double totalVolume;
        private double density;
        private double temperature;
        private double press;
		private double bottomTemperature;
		private double midTemperature;
		private double topTemperature;

		public string TankId { get; private set; }

        /// <summary>
        /// 当前容量
        /// </summary>
        [FieldMapper("VOLM", TypeCode.Int32, TankConstant.CALC_PARAM)]
        public double Volume
        {
            get { return volume; }
            set
            {
                volume = value;
				TankStatusUpdate();
				RaisePropertyChanged("Volume");
            }
        }

		/// <summary>
		/// 液位高
		/// </summary>
		[FieldMapper("LEVEL", TypeCode.Int32, TankConstant.TANKLEVEL_PARAM)]
		public double Level
        {
            get { return level; }
            set
            {
                level = value;
                RaisePropertyChanged("Level");
            }
        }

        /// <summary>
        /// 液位高
        /// </summary>
        [FieldMapper("LEVEL1", TypeCode.Int32, TankConstant.TANKLEVEL_PARAM)]
        public double Level1
        {
            get { return level1; }
            set
            {
                level1 = value;
                RaisePropertyChanged("Level1");
            }
        }
        
        /// <summary>
        /// 速率
        /// </summary>
        [FieldMapper("RATE", TypeCode.Int32, TankConstant.CALC_PARAM)]
        public double Rate
        {
            get { return rate; }
            set
            {
                rate = value;
                RaisePropertyChanged("Rate");
            }
        }

        public double TotalVolume
        {
            get { return totalVolume; }
            set
            {
                totalVolume = value;
                RaisePropertyChanged("TotalVolume");
            }
        }

        /// <summary>
        /// 密度
        /// </summary>
        [FieldMapper("DENSITY", TypeCode.Int32, TankConstant.CALC_PARAM)]
        public double Density
        {
            get { return density; }
            set
            {
                density = value;
                RaisePropertyChanged("Density");
            }
        }

        /// <summary>
        /// 温度
        /// </summary>
        [FieldMapper("TEMP", TypeCode.Int32, TankConstant.TANKTEMP_PARAM)]
        public double Temperature
        {
            get { return temperature; }
            set
            {
				temperature = value;
				RaisePropertyChanged("Temperature");
            }
        }

		/// <summary>
		/// 底部温度
		/// </summary>
		[FieldMapper("TEMP_BOT", TypeCode.Int32, TankConstant.TANKTEMP_PARAM)]
		public double BottomTemperature
		{
			get { return bottomTemperature; }
			set
			{
				bottomTemperature = value;
				TankStatusUpdate();
				RaisePropertyChanged("Temperature");
			}
		}

		/// <summary>
		/// 中部温度
		/// </summary>
		[FieldMapper("TEMP_MID", TypeCode.Int32, TankConstant.TANKTEMP_PARAM)]
		public double MidTemperature
		{
			get { return midTemperature; }
			set
			{
				midTemperature = value;
				TankStatusUpdate();
				RaisePropertyChanged("Temperature");
			}
		}
		/// <summary>
		/// 顶部温度
		/// </summary>
		[FieldMapper("TEMP_TOP", TypeCode.Int32, TankConstant.TANKTEMP_PARAM)]
		public double TopTemperature
		{
			get { return topTemperature; }
			set
			{
				topTemperature = value;
				TankStatusUpdate();              
				RaisePropertyChanged("Temperature");
			}
		}
		/// <summary>
		/// 压力
		/// </summary>
		[FieldMapper("PRESS", TypeCode.Int32, TankConstant.TANKPRESS_PARAM)]
        public double Press
        {
            get { return press; }
            set
            {
                press = value;
                RaisePropertyChanged("Press");
            }
        }

		public void TankStatusUpdate()
		{
            if (level > tempsensor_top_z)
        	    temperature = (bottomTemperature + midTemperature + topTemperature) / 3;
            else if (level > tempsensor_mid_z)
        	    temperature = (bottomTemperature + midTemperature) / 2;
            else
        	    temperature = bottomTemperature;
        /// DEBUG 这里温度的判断暂时以当前体积和最大体积计算百分比做选择，实际需要根据当前液位高选择
        /// 体积变化和任一位置温度变化时都需要重新计算
        //if (volume > totalVolume * 0.8)
        //	temperature = (bottomTemperature + midTemperature + topTemperature) / 3;
        //else if (volume > totalVolume * 0.4)
        //	temperature = (bottomTemperature + midTemperature) / 2;
        //else
        //	temperature = bottomTemperature;
    }
}
}
